//音效引擎，音乐引擎
//音效引擎用法：当前音效停止后播放，如果当前音效停止则播放，取消“当前音效停止后播放”
//音乐引擎用法：与音效引擎相同
var effectEngine = {
	
	_nextOne : null,
	_nextOneLoop : false,
	_nextOneDuration : 2,
	
	PlayOnNext : function(url, loop, duration){
		if (url == null || url == ""){
			return;
		}
		
		effectEngine._nextOne = url;
		effectEngine._nextOneLoop = loop || false;
		effectEngine._nextOneDuration = duration || 1.5;
		
		if (effectEngine._wholeFadeTimer <= 0){
			effectEngine._StartNewOne();
		}
	},
	
	PlayIfStopped : function(url, loop, duration){
		if (url == null || url == ""){
			return;
		}

		if (effectEngine._wholeFadeTimer <= 0){
			effectEngine._nextOne = url;
			effectEngine._nextOneLoop = loop || false;
			effectEngine._nextOneDuration = duration || 1.5;
			
			effectEngine._StartNewOne();
		}
	},
	
	StopLoop : function(){
		if (effectEngine._TimeoutHandler != null){
			clearTimeout(effectEngine._TimeoutHandler);
			effectEngine._TimeoutHandler = null;
		}
	},
	
	_TimeoutHandler : null,

	_StartNewOne : function(){
		effectEngine._wholeFadeTimer = 10;
		cc.audioEngine.playEffect(effectEngine._nextOne, effectEngine._nextOneLoop);
		effectEngine._nextOne = null;
		effectEngine._TimeoutHandler = setTimeout(effectEngine._DurationTimeout, effectEngine._nextOneDuration * 1000);
	},

	_DurationTimeout : function(){
		effectEngine._FadeOut();
	},
	
	_wholeFadeTimer : 0,
	_fadeTimer : 0,
	_fadeIntervalHandler : null,
	
	_FadeOut : function(fadeTime, waitTime){
		
		fadeTime = fadeTime || 2;
		if (fadeTime < 0.1)
			fadeTime = 0.1;
		
		waitTime = waitTime || 1;
		if (waitTime < 0)
			waitTime = 0;
		
		effectEngine._fadeTimer = 10;
		effectEngine._wholeFadeTimer = 10 * (fadeTime + waitTime) / fadeTime;
		effectEngine._fadeIntervalHandler = setInterval(effectEngine._FadeOutInterval, fadeTime / 10 * 1000);
	},
	
	_FadeOutInterval : function(){
		if (--effectEngine._fadeTimer <= 0){
			if (effectEngine._fadeTimer + 1 > 0){
				cc.audioEngine.stopAllEffects();
				cc.audioEngine.setEffectsVolume(setting.effectVolume * 0.2);
			}
		}else{
			cc.audioEngine.setEffectsVolume(setting.effectVolume * 0.2 * effectEngine._fadeTimer * 0.1);
		}
		
		if (--effectEngine._wholeFadeTimer <= 0){
			clearInterval(effectEngine._fadeIntervalHandler);
			if (effectEngine._nextOne != null){
				effectEngine._StartNewOne();
			}
		}
	},
};


var musicEngine = {

	_nextOne : null,
	_nextOneLoop : false,
	_nextOneDuration : 120,

	PlayOnNext : function(url, loop, duration){
		if (url == null || url == ""){
			return;
		}

		musicEngine._nextOne = url;
		musicEngine._nextOneLoop = loop || false;
		musicEngine._nextOneDuration = duration || 120;

		if (musicEngine._wholeFadeTimer <= 0){
			musicEngine._StartNewOne();
		}
	},
	
	PlayIfStopped : function(url, loop, duration){
		if (url == null || url == ""){
			return;
		}
		if (musicEngine._wholeFadeTimer <= 0){
			musicEngine._nextOne = url;
			musicEngine._nextOneLoop = loop || false;
			musicEngine._nextOneDuration = duration || 120;
			
			musicEngine._StartNewOne();
		}
	},
	
	StopLoop : function(){
		if (musicEngine._TimeoutHandler != null){
			clearTimeout(musicEngine._TimeoutHandler);
			musicEngine._TimeoutHandler = null;
		}
	},

	_TimeoutHandler : null,

	_StartNewOne : function(){
		musicEngine._wholeFadeTimer = 10;
		cc.audioEngine.playMusic(musicEngine._nextOne, musicEngine._nextOneLoop);
		musicEngine._nextOne = null;
		musicEngine._TimeoutHandler = setTimeout(musicEngine._DurationTimeout, musicEngine._nextOneDuration * 1000);
	},

	_DurationTimeout : function(){
		musicEngine._FadeOut();
	},

	_wholeFadeTimer : 0,
	_fadeTimer : 0,
	_fadeIntervalHandler : null,

	_FadeOut : function(fadeTime, waitTime){

		fadeTime = fadeTime || 10;
		if (fadeTime < 0.1)
			fadeTime = 0.1;

		waitTime = waitTime || 10;
		if (waitTime < 0)
			waitTime = 0;

		musicEngine._fadeTimer = 10;
		musicEngine._wholeFadeTimer = 10 * (fadeTime + waitTime) / fadeTime;
		musicEngine._fadeIntervalHandler = setInterval(musicEngine._FadeOutInterval, fadeTime / 10 * 1000);
	},

	_FadeOutInterval : function(){
		if (--musicEngine._fadeTimer <= 0){
			if (musicEngine._fadeTimer + 1 > 0){
				cc.audioEngine.stopMusic(false);
				cc.audioEngine.setMusicVolume(setting.musicVolume * 0.2);
			}
		}else{
			cc.audioEngine.setMusicVolume(setting.musicVolume * 0.2 * musicEngine._fadeTimer * 0.1);
		}

		if (--musicEngine._wholeFadeTimer <= 0){
			clearInterval(musicEngine._fadeIntervalHandler);
			if (musicEngine._nextOne != null){
				musicEngine._StartNewOne();
			}
		}
	},
};